<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use PHPExcel;
use PHPExcel_IOFactory;
use PHPExcel_Shared_Date;
use PHPExcel_Style;
use PHPExcel_Style_Alignment;
use PHPExcel_Style_Border;
use PHPExcel_Style_Fill;
use PHPExcel_Style_NumberFormat;

/**
 * 补货管理
 *
 * @icon fa fa-circle-o
 */
class Replen extends Backend {

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $otherwhere[] = ['type', 'BETWEEN', '2,3'];
            $filterAlias['user_id'] = 'o.user_id';
            $filterAlias['type'] = 'o.type';
            $filterAlias['status'] = 'o.status';
            $filterAlias['createtime'] = 'o.createtime';
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, null, $otherwhere, $filterAlias);
            $admin_info = db('admin')->where('id', $this->auth->id)->find();
            $admin_where = [];

            if ($admin_info['identity'] == 1) {
                if ($admin_info['province'] != 0) {
                    $admin_where['fa_user_box.province'] = $admin_info['province'];
                }
                if ($admin_info['city'] != 0) {
                    $admin_where['fa_user_box.city'] = $admin_info['city'];
                }
                if ($admin_info['area'] != 0) {
                    $admin_where['fa_user_box.area'] = $admin_info['area'];
                }
                if ($admin_info['quarters_id'] != 0) {
                    $admin_where['fa_user_box.quarters_id'] = $admin_info['quarters_id'];
                }
                if ($admin_info['unit_id'] != 0) {
                    $admin_where['fa_user_box.unit_id'] = $admin_info['unit_id'];
                }
                if ($admin_info['room_id'] != 0) {
                    $admin_where['fa_user_box.room_id'] = $admin_info['room_id'];
                }
            } else if ($admin_info['identity'] == 2) {
                $admin_where['fa_user_box.box_no'] = ['in', $admin_info['box_nos']];
            }
            $total = $this->model
                    ->alias('o')
                    ->join('fa_user_box', 'fa_user_box.user_box_id = o.user_box_id', 'left')
                    ->where($where)
                    ->where($admin_where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->alias('o')
                    ->field('o.*,fa_user_box.box_no,fa_user_box.province_name,fa_user_box.city_name,fa_user_box.area_name,fa_user_box.quarters_id,fa_user_box.room_id,fa_user_box.unit_id,u.nick_name as contacts,u.phone as telephone,fa_user_box.remark,fa_user_box.sex')
                    ->join('fa_user_box', 'fa_user_box.user_box_id = o.user_box_id', 'left')
                    ->join('fa_users u', 'o.user_id = u.user_id', 'left')
                    ->where($where)
                    ->where($admin_where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();


            $list = collection($list)->toArray();

            if ($list) {
                foreach ($list as &$val) {

                    if ($val['sex'] == 0) {
                        $val['sex'] = '女';
                    } else {
                        $val['sex'] = '男';
                    }
                    $val['room_name'] = db('house')->where(['house_id' => $val['room_id']])->value('house_name');
                    $val['unit_name'] = db('house')->where(['house_id' => $val['unit_id']])->value('house_name');
                    $val['quarters_name'] = db('house')->where(['house_id' => $val['quarters_id']])->value('house_name');
                    $val['buy_num'] = db('order_goods')->where(['order_id' => $val['order_id']])->sum('buy_num');
                }
                unset($val);
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function detail() {
        $id = $this->request->param('ids');
        if (empty($id)) {
            $this->error('请确认要补货的订单');
        }

        $order_info = $this->model
                ->alias('o')
                ->field('o.*,fa_user_box.box_no,fa_user_box.province_name,fa_user_box.city_name,fa_user_box.area_name,fa_user_box.quarters_id,fa_user_box.room_id,fa_user_box.unit_id,fa_user_box.contacts,fa_user_box.telephone,fa_user_box.remark')
                ->join('fa_user_box', 'fa_user_box.user_box_id = o.user_box_id', 'left')
                ->where('o.order_id', $id)
                ->where('o.type', 'BETWEEN', '2,3')
                ->find();

        if ($order_info) {
            $order_info['room_name'] = db('house')->where(['house_id' => $order_info['room_id']])->value('house_name');
            $order_info['unit_name'] = db('house')->where(['house_id' => $order_info['unit_id']])->value('house_name');
            $order_info['quarters_name'] = db('house')->where(['house_id' => $order_info['quarters_id']])->value('house_name');
            $order_info['buy_num'] = db('order_goods')->where(['order_id' => $order_info['order_id']])->sum('buy_num');

            $user_info = db('users')->where('user_id', $order_info['user_id'])->find();
            if (empty($user_info)) {
                $order_info['nick_name'] = '';
                $order_info['phone'] = '';
            } else {
                $order_info['nick_name'] = $user_info['nick_name'];
                $order_info['phone'] = $user_info['phone'];
            }
        } else {
            $this->error('没有找到订单信息');
        }


        //用户申请补货的商品
        $list = db('order_goods')
                ->field('fa_order_goods.*,fa_goods.goods_weight,fa_goods.purchase_price,fa_goods.selling_price as goods_price,fa_goods_cate.cate_title')
                ->join('fa_goods', 'fa_order_goods.goods_id = fa_goods.goods_id', 'left')
                ->join('fa_goods_cate', 'fa_goods_cate.cate_id = fa_goods.cate_id', 'left')
                ->where('order_id', $id)
                ->select();

        //实际补货的商品
        $replen_list = db('replen_goods')
                ->field('fa_replen_goods.*,fa_goods.goods_weight,fa_goods.purchase_price,fa_goods.selling_price as goods_price,fa_goods_cate.cate_title')
                ->join('fa_goods', 'fa_replen_goods.goods_id = fa_goods.goods_id', 'left')
                ->join('fa_goods_cate', 'fa_goods_cate.cate_id = fa_goods.cate_id', 'left')
                ->where('order_id', $id)
                ->select();


        $this->assign('replen_list', $replen_list);
        $this->assign('list', $list);
        $this->assign('info', $order_info);
        $this->assign('ids', $id);
        return $this->view->fetch();
    }

    //完成补货
    public function replen() {
        $id = $this->request->param('ids');


        if ($this->request->isPost()) {
            $order_id = input('order_id', 0);
            if ($order_id == 0) {
                $this->error('请确认要补货的订单');
            }

            //取出该订单的所有补货商品

            $replen_goods_list = db('replen_goods')->where('order_id', $id)->where('is_replen', 0)->select();

            if (empty($replen_goods_list)) {
                $this->error('没有要补货的商品');
            }

            $order_info = db('order')->where('order_id', $id)->find();

            if ($order_info['type'] == 3) {//边吃边付才要加库存
                //添加库存变动流水
                $stock_log['change_matters'] = 2;
                $stock_log['replen_id'] = $id;

                foreach ($replen_goods_list as $val) {

                    $stock_log['change_type'] = 0;
                    if ($val['buy_num'] > 0) {
                        $stock_log['change_type'] = 1;
                    }

                    $user_box_goods_info = db('user_box_goods')->where(['goods_id' => $val['goods_id'], 'user_box_id' => $order_info['user_box_id']])->find();

                    if ($user_box_goods_info) {
                        db('user_box_goods')->where(['goods_id' => $val['goods_id'], 'user_box_id' => $order_info['user_box_id']])->setInc('stock_num', $val['buy_num']);
                        db('user_box_goods')->where(['goods_id' => $val['goods_id'], 'user_box_id' => $order_info['user_box_id']])->setInc('total_num', $val['buy_num']);
                        $stock_log['box_goods_id'] = $user_box_goods_info['box_goods_id'];
                        $stock_log['quantity'] = $user_box_goods_info['stock_num'] + $val['buy_num'];
                    } else {
                        $user_box_goods['goods_id'] = $val['goods_id'];
                        $user_box_goods['user_box_id'] = $order_info['user_box_id'];
                        $user_box_goods['goods_id'] = $val['goods_id'];

                        $quarters_id = db('user_box')->where('user_box_id', $order_info['user_box_id'])->value('quarters_id');

                        $user_box_goods['selling_price'] = db('house_goods')->where(['house_id' => $quarters_id, 'goods_id' => $val['goods_id']])->value('selling_price');
                        $user_box_goods['stock_num'] = $val['buy_num'];
                        $user_box_goods['total_num'] = $val['buy_num'];
                        $stock_log['quantity'] = $val['buy_num'];
                        $stock_log['box_goods_id'] = db('user_box_goods')->insertGetId($user_box_goods);
                    }
                    $stock_log['goods_id'] = $val['goods_id'];
                    $stock_log['change_amount'] = $val['buy_num'];

                    db('stock_log')->insert($stock_log);
                }
            }

            $orderData['updatetime'] = date('Y-m-d H:i:s');
            $orderData['status'] = 10;
            db('order')->where('order_id', $id)->update($orderData);
            db('replen_goods')->where('order_id', $id)->where('is_replen', 0)->update(['is_replen' => 1]);
            $this->success();
        } else {

            if (empty($id)) {
                $this->error('请确认要补货的订单');
            }

            //先删除改原有的补货表记录
            db('replen_goods')->where('order_id', $id)->where('is_replen', 0)->delete();

            //添加补货订单内的商品进补货表

            $order_goods_list = db('order_goods')->field('order_id,goods_id,box_goods_id,goods_title,goods_logo,goods_model,goods_spec,brand_title,selling_price,buy_num')->where('order_id', $id)->select();

            if ($order_goods_list) {
                db('replen_goods')->insertAll($order_goods_list);
            } else {
                $this->error('改订单没有要补货的商品');
            }

            $order_info = $this->model
                    ->alias('o')
                    ->field('o.*,fa_user_box.box_no,fa_user_box.province_name,fa_user_box.city_name,fa_user_box.area_name,fa_user_box.quarters_id,fa_user_box.room_id,fa_user_box.unit_id,fa_user_box.contacts,fa_user_box.telephone,fa_user_box.remark')
                    ->join('fa_user_box', 'fa_user_box.user_box_id = o.user_box_id', 'left')
                    ->where('o.order_id', $id)
                    ->where('o.type', 'BETWEEN', '2,3')
                    ->find();

            if ($order_info) {
                $order_info['room_name'] = db('house')->where(['house_id' => $order_info['room_id']])->value('house_name');
                $order_info['unit_name'] = db('house')->where(['house_id' => $order_info['unit_id']])->value('house_name');
                $order_info['quarters_name'] = db('house')->where(['house_id' => $order_info['quarters_id']])->value('house_name');
                $order_info['buy_num'] = db('order_goods')->where(['order_id' => $order_info['order_id']])->sum('buy_num');

                $user_info = db('users')->where('user_id', $order_info['user_id'])->find();
                if (empty($user_info)) {
                    $order_info['nick_name'] = '';
                    $order_info['phone'] = '';
                } else {
                    $order_info['nick_name'] = $user_info['nick_name'];
                    $order_info['phone'] = $user_info['phone'];
                }

                //边吃边付的订单价格另外计算
                if ($order_info['type'] == 3) {
                    $total_price = db('order_goods')->fieldRaw('SUM(buy_num*selling_price) as total_price')->where('order_id', $id)->find();
                    $order_info['goods_price'] = $total_price['total_price'];
                }
            } else {
                $this->error('没有找到订单信息');
            }

            $this->assign('info', $order_info);
            $this->assign('id', $id);
        }
        return $this->view->fetch();
    }

    public function goodslist() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $order_id = $this->request->param('ids');
            $otherwhere[] = ['order_id', '=', $order_id];

            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, null, $otherwhere);
            $total = db('replen_goods')
                    ->join('fa_goods', 'fa_replen_goods.goods_id = fa_goods.goods_id', 'left')
                    ->join('fa_goods_cate', 'fa_goods_cate.cate_id = fa_goods.cate_id', 'left')
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = db('replen_goods')
                    ->field('fa_replen_goods.*,fa_goods.goods_weight,fa_goods.purchase_price,fa_goods.selling_price as goods_price,fa_goods_cate.cate_title')
                    ->join('fa_goods', 'fa_replen_goods.goods_id = fa_goods.goods_id', 'left')
                    ->join('fa_goods_cate', 'fa_goods_cate.cate_id = fa_goods.cate_id', 'left')
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            $list = collection($list)->toArray();

            foreach ($list as &$val) {
                $order_info = db('order')->where('order_id', $val['order_id'])->find();
                $user_box_info = db('user_box')->where('user_box_id', $order_info['user_box_id'])->find();
                $val['selling_price'] = db('house_goods')->where(['house_id' => $user_box_info['quarters_id'], 'goods_id' => $val['goods_id']])->value('selling_price');
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
//        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function addgoods() {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $order_id = $this->request->param('ids');

            $order_info = db('order')->where('order_id', $order_id)->find();

            $quarters_id = db('user_box')->where('user_box_id', $order_info['user_box_id'])->value('quarters_id');

            //去掉已添加的商品
            $box_goods_ids = db('replen_goods')->where('order_id', $order_id)->group('goods_id')->column('goods_id');


            $goods_ids = db('house_goods')->where(['house_id' => $quarters_id, 'goods_id' => ['not in', $box_goods_ids]])->column('goods_id');

            if ($goods_ids) {
                $otherwhere[] = ['goods_id', 'in', $goods_ids];

//            $otherwhere[] = ['house_type', '=', 1];
                $otherwhere[] = ['is_deleted', '=', 0];
                list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, null, $otherwhere);
                $total = db('goods')
                        ->where($where)
                        ->order($sort, $order)
                        ->count();
                $list = db('goods')
                        ->where($where)
                        ->order($sort, $order)
                        ->limit($offset, $limit)
                        ->select();


                $list = collection($list)->toArray();
                if ($list) {
                    foreach ($list as &$val) {
                        $val['brand_title'] = db('goods_brand')->where(['brand_id' => $val['brand_id']])->value('brand_title');
                        $val['cate_title'] = db('goods_cate')->where(['cate_id' => $val['cate_id']])->value('cate_title');
                    }
                    unset($val);
                }
                $result = array("total" => $total, "rows" => $list);

                return json($result);
            } else {
                $result = array("total" => 0, "rows" => []);
                return json($result);
            }
        }

        return $this->view->fetch();
    }

    public function addreplengoods() {
        $id = input('goods_id', 0, 'intval');
        $order_id = $this->request->param('ids');

        $is_exis_goods = db('replen_goods')->where('order_id', $order_id)->where('goods_id', $id)->find();
        if ($is_exis_goods) {
            //已经存在的商品就不用再次处理了
            $this->success();
        }
        $order_info = db('order')->where('order_id', $order_id)->find();

        $quarters_id = db('user_box')->where('user_box_id', $order_info['user_box_id'])->value('quarters_id');

        $data['goods_id'] = $id;

        $data['selling_price'] = db('house_goods')->where(['house_id' => $quarters_id, 'goods_id' => $id])->value('selling_price');
        $goods_info = Db::name('goods')
                ->alias('g')
                ->field('g.goods_id,g.goods_title,g.goods_model,g.goods_spec,g.goods_logo,gb.brand_title')
                ->join('fa_goods_brand gb', 'g.brand_id = gb.brand_id')
                ->where(['g.goods_id' => $id, 'g.status' => 1])
                ->find();
        $data['goods_title'] = $goods_info['goods_title'];
        $data['goods_model'] = $goods_info['goods_model'];
        $data['goods_spec'] = $goods_info['goods_spec'];
        $data['goods_logo'] = $goods_info['goods_logo'];
        $data['brand_title'] = $goods_info['brand_title'];

        $data['order_id'] = $order_id;
        db('replen_goods')->insert($data);

        $this->success();
    }

    public function delgoods() {
        $id = input('id', 0, 'intval');
        $order_id = $this->request->param('ids');
        db('replen_goods')->where(['goods_id' => $id, 'order_id' => $order_id, 'is_replen' => 0])->delete();
        $this->success();
    }

    public function editnum() {

        $ids = input('ids');
        $goods_id = input('goods_id', 0, 'intval');
        $arrData['buy_num'] = input('buy_num', 0, 'intval');
        db('replen_goods')->where(['goods_id' => $goods_id, 'order_id' => ['in', $ids]])->update($arrData);
        $this->success();
    }

    public function export() {
        if ($this->request->isPost()) {
            set_time_limit(0);
            $search = $this->request->post('search');
            $ids = $this->request->post('ids');
            $filter = $this->request->post('filter');
            $op = $this->request->post('op');
            $columns = $this->request->post('columns');

            $excel = new PHPExcel();

            $excel->getProperties()
                    ->setCreator("MOHEBIANLI")
                    ->setLastModifiedBy("MOHEBIANLI")
                    ->setTitle("补货订单")
                    ->setSubject("Subject");
            $excel->getDefaultStyle()->getFont()->setName('Microsoft Yahei');
            $excel->getDefaultStyle()->getFont()->setSize(12);

            $this->sharedStyle = new PHPExcel_Style();
            $this->sharedStyle->applyFromArray(
                    array(
                        'fill' => array(
                            'type' => PHPExcel_Style_Fill::FILL_SOLID,
                            'color' => array('rgb' => '000000')
                        ),
                        'font' => array(
                            'color' => array('rgb' => "000000"),
                        ),
                        'alignment' => array(
                            'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                            'indent' => 1
                        ),
                        'borders' => array(
                            'allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                        )
            ));

            $worksheet = $excel->setActiveSheetIndex(0);
            $worksheet->setTitle('补货订单数据');

            $whereIds = $ids == 'all' ? '1=1' : ['order_id' => ['in', explode(',', $ids)]];
            $this->request->get(['search' => $search, 'ids' => $ids, 'filter' => $filter, 'op' => $op]);
            $otherwhere[] = ['type', 'BETWEEN', '2,3'];
            $filterAlias['user_id'] = 'o.user_id';
            $filterAlias['type'] = 'o.type';
            $filterAlias['status'] = 'o.status';
            $filterAlias['createtime'] = 'o.createtime';
            $filterAlias['province'] = 'ub.province';
            $filterAlias['city'] = 'ub.city';
            $filterAlias['area'] = 'ub.area';
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null, null, $otherwhere, $filterAlias);

            $line = 1;
            $list = [];
            $this->model
                    ->alias('o')
                    ->field('o.order_id,o.type,o.order_no,ub.box_no,ub.province_name,ub.city_name,ub.area_name,h1.house_name as quarters_name,h2.house_name as unit_name,h3.house_name as room_name," " as goods ,o.goods_price,o.pay_price,u.nick_name as contacts,u.phone as telephone,o.pay_type,o.status,o.createtime,o.pay_at,o.updatetime')
                    ->join('fa_user_box ub', 'ub.user_box_id = o.user_box_id', 'left')
                    ->join('fa_house h1', 'ub.quarters_id = h1.house_id', 'left')
                    ->join('fa_house h2', 'ub.unit_id = h2.house_id', 'left')
                    ->join('fa_house h3', 'ub.room_id = h3.house_id', 'left')
                    ->join('fa_users u', 'o.user_id = u.user_id', 'left')
                    ->where($where)
                    ->where($whereIds)
                    ->chunk(100, function ($items) use (&$list, &$line, &$worksheet) {
                        $order_ids = [];
                        foreach ($items as $row) {
                            $order_ids[] = $row['order_id'];
                        }

                        $order_goods = [];

                        if ($order_ids) {
                            $goods_list = db('order_goods')
                                    ->alias('og')
                                    ->field('og.order_id,o.goods_title,og.buy_num')
                                    ->join('fa_goods o', 'og.goods_id=o.goods_id')
                                    ->where('og.order_id', 'in', $order_ids)
                                    ->select();

                            if ($goods_list) {
                                foreach ($goods_list as $val) {
                                    $order_goods[$val['order_id']][] = $val;
                                }
                            }
                        }

                        $styleArray = array(
                            'font' => array(
                                'color' => array('rgb' => '000000'),
                                'size' => 10,
                                'name' => 'Verdana'
                        ));
                        $list = $items = collection($items)->toArray();
                        foreach ($items as $index => &$item) {
                            $item['pay_type'] = $item['pay_type_text'];
                            $item['status'] = $item['status_text'];
                            $item['type'] = $item['type_text'];

                            $item['room_name'] = $item['province_name'] . '-' . $item['city_name'] . '-' . $item['area_name'] . '-' . $item['quarters_name'] . '-' . $item['unit_name'] . '-' . $item['room_name'];
                            unset($item['province_name']);
                            unset($item['city_name']);
                            unset($item['area_name']);
                            unset($item['quarters_name']);
                            unset($item['unit_name']);
                            unset($item['pay_type_text']);
                            unset($item['status_text']);
                            unset($item['type_text']);

                            if (isset($order_goods[$item['order_id']])) {
                                foreach ($order_goods[$item['order_id']] as $val) {
                                    $item['goods'] .= $val['goods_title'] . ' x' . $val['buy_num'] . chr(10);
                                }
                            }
                            $item['goods'] = trim($item['goods']);
                            $line++;
                            $col = 0;
                            foreach ($item as $field => $value) {

                                $worksheet->setCellValueByColumnAndRow($col, $line, $value);
                                $worksheet->getStyleByColumnAndRow($col, $line)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                                $worksheet->getCellByColumnAndRow($col, $line)->getStyle()->applyFromArray($styleArray);
                                $col++;
                            }
                        }

                        unset($item);
                        $worksheet->getStyle('F0:F100000')->getAlignment()->setWrapText(true);
                        $worksheet->getColumnDimension('F')->setWidth(30);
                        $worksheet->getColumnDimension('B')->setWidth(16);
                        $worksheet->getColumnDimension('C')->setWidth(15);
                        $worksheet->getColumnDimension('D')->setWidth(15);
                        $worksheet->getColumnDimension('E')->setWidth(40);
                        $worksheet->getColumnDimension('I')->setWidth(15);
                        $worksheet->getColumnDimension('J')->setWidth(15);
                    });
            $first = array_keys($list[0]);
            unset($first[4]);
            unset($first[5]);
            unset($first[6]);
            unset($first[7]);
            unset($first[8]);
            
            foreach ($first as $index => $item) {
                if ($item == 'province_name') {
                    continue;
                }
                if ($item == 'city_name') {
                    continue;
                }
                if ($item == 'area_name') {
                    continue;
                }
                if ($item == 'quarters_name') {
                    continue;
                }
                if ($item == 'unit_name') {
                    continue;
                }
                if ($item == 'pay_type_text') {
                    continue;
                }
                if ($item == 'status_text') {
                    continue;
                }
                if ($item == 'type_text') {
                    continue;
                }
                
                if($index>8){
                    $index = $index-5;
                }
                $worksheet->setCellValueByColumnAndRow($index, 1, __($item));
            }

            $excel->createSheet();
            // Redirect output to a client’s web browser (Excel2007)
            $title = '补货订单' . date("YmdHis");
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
            header('Cache-Control: max-age=0');
            // If you're serving to IE 9, then the following may be needed
            header('Cache-Control: max-age=1');

            // If you're serving to IE over SSL, then the following may be needed
            header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
            header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
            header('Pragma: public'); // HTTP/1.0

            $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
            $objWriter->save('php://output');
            return;
        }
    }

}
